package com.naizhuai.ui.components;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.util.NaizhuAILog;

/**
 * Created by code_han on 2018/3/14.
 */

public class SingleLabelsView extends RelativeLayout implements View.OnClickListener, View.OnLongClickListener {
    private Context mContext;
    private TextView label;

    private DeleteForkListener deleteListener;
    private ImageView fork;
    private ImageView hook;

    private boolean showHook;

    private boolean showFork;

    public boolean isShowHook() {
        return showHook;
    }

    public void setShowHook(boolean showHook) {
        this.showHook = showHook;
    }

    public boolean isShowFork() {
        return showFork;
    }

    public void setShowFork(boolean showFork) {
        this.showFork = showFork;
    }

    private static final int IMAGE_WIDTH = 32;
    private static final int IMAGE_HEIGHT = 32;

    private int forkWidth = 0;
    private int forkHeight = 0;

    private int hookWidth = 0;
    private int hookHeight = 0;

    public TextView getLabel() {
        return label;
    }

    public void setLabel(TextView label) {
        this.label = label;
    }

    public ImageView getFork() {
        return fork;
    }

    public void setFork(ImageView fork) {
        this.fork = fork;
    }

    public SingleLabelsView(Context context) {
        this(context, null);
    }

    public SingleLabelsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SingleLabelsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        label = new TextView(mContext);
        addView(label);
        Bitmap forkBit = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.delete_fork);
        fork = initImageView(true, forkBit);
        forkWidth = forkBit.getWidth();
        forkHeight = forkBit.getHeight();
        addView(fork);
        fork.setVisibility(INVISIBLE);
        Bitmap hookBit = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.checkbox_pressed);
        hookWidth = forkBit.getWidth();
        hookHeight = forkBit.getHeight();
        hook = initImageView(false, hookBit);
        addView(hook);
        RelativeLayout.LayoutParams labelParams = (LayoutParams) label.getLayoutParams();
        labelParams.topMargin = forkHeight / 4;
        label.setLayoutParams(labelParams);

        hook.setVisibility(INVISIBLE);
        initListener();
    }

    private void initViewPosition() {
        LayoutParams forkParams = (LayoutParams) fork.getLayoutParams();
        LayoutParams hookParams = (LayoutParams) hook.getLayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            forkParams.setMarginStart(label.getMeasuredWidth() - forkWidth / 2);
            hookParams.setMarginStart(label.getMeasuredWidth() - hookWidth / 2);
        }else{
            forkParams.leftMargin=(label.getMeasuredWidth() - forkWidth / 2);
            hookParams.leftMargin=(label.getMeasuredWidth() - hookWidth / 2);
        }
        fork.setLayoutParams(forkParams);
        hook.setLayoutParams(hookParams);
    }

    private ImageView initImageView(boolean needClick, Bitmap resource) {
        ImageView image = new ImageView(mContext);
        image.setImageBitmap(resource);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT);
        image.setLayoutParams(params);
        if (needClick)
            image.setOnClickListener(this);
        return image;
    }


    private void initListener() {
        setOnLongClickListener(this);
    }


    @Override
    public boolean onLongClick(View v) {
        showForkIcon(!isShowDeleteFork());
        return false;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

    }


    public void setTextSize(int type, float size) {
        label.setTextSize(type, size);
    }

    public void setTextColor(ColorStateList colors) {
        label.setTextColor(colors);
    }

    public void setText(CharSequence text) {
        label.setText(text);
        label.post(new Runnable() {
            @Override
            public void run() {
                initViewPosition();
            }
        });
    }

    public void setTextPadding(int left, int top, int right, int bottom) {
        label.setPadding(left, top, right, bottom);
    }

    public void setTextBackgroundResource(int resource) {
        label.setBackgroundResource(resource);
    }

    public void showForkIcon(boolean show) {
        if (!showFork)
            return;
        if (show)
            forkShowAnimation(fork);
        else
            forkMissAnimation(fork);
    }

    public void setDeleteListener(DeleteForkListener listener) {
        this.deleteListener = listener;
    }

    public boolean isShowDeleteFork() {
        return fork.getVisibility() != INVISIBLE;
    }

    public boolean isTextSelected() {
        return label.isSelected();
    }


    public void setTextSelected(boolean show) {
        label.setSelected(show);
        if (!showHook)
            return;
        if (show)
            forkShowAnimation(hook);
        else
            forkMissAnimation(hook);
    }

    @Override
    public void onClick(View v) {
        if (deleteListener != null)
            deleteListener.deleteFork(this);
    }


    public interface DeleteForkListener {
        void deleteFork(SingleLabelsView singleView);
    }
    private void canClick(boolean click){
        this.setClickable(click);
    }

    private void forkShowAnimation(final ImageView image) {
        AnimatorSet animatorSet = new AnimatorSet();//组合动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(image, "scaleX", 0, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(image, "scaleY", 0, 1f);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                canClick(false);
                image.setVisibility(VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                canClick(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                canClick(true);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.setDuration(300);
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animatorSet.play(scaleX).with(scaleY);//两个动画同时开始
        animatorSet.start();

    }

    private void forkMissAnimation(final ImageView image) {
        AnimatorSet animatorSet = new AnimatorSet();//组合动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(image, "scaleX", 1f, 0);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(image, "scaleY", 1f, 0);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                canClick(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                canClick(true);
                image.setVisibility(INVISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                canClick(true);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.setDuration(300);
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animatorSet.play(scaleX).with(scaleY);//两个动画同时开始
        animatorSet.start();
    }

}
